Imports System.Collections.Generic
Imports System.IO
Imports System.Windows.Forms

Imports Vintasoft.Pdf
Imports Vintasoft.Pdf.Tree.Fonts

Imports DemosCommonCode.Pdf.Security

Namespace Pdf
	Public Partial Class CreateFontForm
		Inherits Form

		#Region "Fields"

		Private _document As PdfDocument
		Private _selectedDocument As PdfDocument
		Private _tempDocument As PdfDocument
		Private _ttfStream As Stream

		#End Region



		#Region "Constructors"

		Public Sub New()
			InitializeComponent()
		End Sub

		Public Sub New(document As PdfDocument)
			Me.New()
			_tempDocument = New PdfDocument(PdfFormat.Pdf_14)
			_document = document
			fontTypeComboBox.SelectedIndex = 1

			standardFontRadioButton.Checked = True
		End Sub

		#End Region



		#Region "Properties"

		Private _newFont As PdfFont
		Public ReadOnly Property NewFont() As PdfFont
			Get
				Return _newFont
			End Get
		End Property

		#End Region



		#Region "Methods"

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			_tempDocument.Dispose()
			If systemFontRadioButton.Checked Then
				Dim fontName As String = DirectCast(fontsComboBox.SelectedItem, String)
				Try
					If fontTypeComboBox.SelectedIndex = 0 Then
						_newFont = _document.FontManager.CreateSimpleFontFromTrueTypeFont(PdfFontManager.GetSystemTrueTypeFontFileName(fontName))
					Else
						_newFont = _document.FontManager.CreateCIDFontFromTrueTypeFont(PdfFontManager.GetSystemTrueTypeFontFileName(fontName))
					End If
				Catch ex As Exception
					DemosTools.ShowErrorMessage(ex)
					Return
				End Try
			ElseIf fromTTFRadioButton.Checked Then
				If _ttfStream Is Nothing Then
					Return
				End If
				_ttfStream.Position = 0
				If fontTypeComboBox.SelectedIndex = 0 Then
					_newFont = _document.FontManager.CreateSimpleFontFromTrueTypeFont(_ttfStream)
				Else
					_newFont = _document.FontManager.CreateCIDFontFromTrueTypeFont(_ttfStream)
				End If
			ElseIf fromPDFDocumentRadioButton.Checked Then
				If _selectedDocument Is Nothing Then
					Return
				End If
				_newFont = _document.FontManager.CreateFontCopy(_selectedDocument.Fonts(fontsComboBox.SelectedIndex))
			ElseIf standardFontRadioButton.Checked Then
				_newFont = _document.FontManager.GetStandardFont(DirectCast(fontsComboBox.SelectedItem, StandardFontType))
			End If
			DialogResult = DialogResult.OK
		End Sub

		Private Sub fontsComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
			SetFontInFontViewer()
		End Sub

		Private Sub fromTTFRadioButton_CheckedChanged(sender As Object, e As EventArgs)
			If _ttfStream IsNot Nothing Then
				_ttfStream.Close()
				_ttfStream.Dispose()
			End If
			If fromTTFRadioButton.Checked Then
				fontTypeGroupBox.Visible = True
				If openTTFFileDialog.ShowDialog() = DialogResult.OK Then
					Try
						_ttfStream = File.OpenRead(openTTFFileDialog.FileName)
						SetFontInFontViewer()
					Catch ex As Exception
						pdfFontViewerControl.PdfFont = Nothing
						DemosTools.ShowErrorMessage(ex)
					End Try
				Else
					pdfFontViewerControl.PdfFont = Nothing
				End If
			End If
		End Sub

		Private Sub systemFontRadioButton_CheckedChanged(sender As Object, e As EventArgs)
			If systemFontRadioButton.Checked Then
				fontTypeGroupBox.Visible = True
				fontsComboBox.Items.Clear()
				fontsComboBox.SelectedIndex = -1
				Dim fontNames As String() = PdfFontManager.GetSystemTrueTypeFontNames()
				For Each fontName As String In fontNames
					fontsComboBox.Items.Add(fontName)
				Next
				fontsComboBox.SelectedIndex = 0
			End If
		End Sub

		Private Sub AddFontForm_FormClosing(sender As Object, e As FormClosingEventArgs)
			FreeResources()
		End Sub

		Private Sub fromPDFDocumentRadioButton_CheckedChanged(sender As Object, e As EventArgs)
			fontsComboBox.Items.Clear()
			If _selectedDocument IsNot Nothing Then
				_selectedDocument.Dispose()
				_selectedDocument = Nothing
				pdfFontViewerControl.PdfFont = Nothing
			End If
			If fromPDFDocumentRadioButton.Checked Then
				fontTypeGroupBox.Visible = False
				If openPdfDocumentDialog.ShowDialog() = DialogResult.OK Then
					Try
						_selectedDocument = New PdfDocument(openPdfDocumentDialog.FileName)
						If PasswordForm.Authenticate(_selectedDocument) Then
							Dim fonts As IList(Of PdfFont) = _selectedDocument.Fonts
							If fonts.Count > 0 Then
								For i As Integer = 0 To fonts.Count - 1
									fontsComboBox.Items.Add(String.Format("[№{1}] {0}", fonts(i).FontName, fonts(i).ObjectNumber))
								Next
								fontsComboBox.SelectedIndex = 0
							Else
								DemosTools.ShowWarningMessage("This document does not contain fonts.")
								_selectedDocument.Dispose()
								_selectedDocument = Nothing
							End If
						Else
							_selectedDocument.Dispose()
							_selectedDocument = Nothing

						End If
					Catch ex As Exception
						pdfFontViewerControl.PdfFont = Nothing
						DemosTools.ShowErrorMessage(ex)
					End Try
				End If
			End If
		End Sub

		Private Sub standardFontRadioButton_CheckedChanged(sender As Object, e As EventArgs)
			fontsComboBox.Items.Clear()
			If standardFontRadioButton.Checked Then
				fontTypeGroupBox.Visible = False
				fontsComboBox.SelectedIndex = -1
				For i As Integer = 0 To PdfFont.StandardFontsCount - 1
					fontsComboBox.Items.Add(DirectCast(i, StandardFontType))
				Next
				fontsComboBox.SelectedIndex = 0
			End If
		End Sub

		Private Sub fontTypeComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
			SetFontInFontViewer()
		End Sub

		Private Sub SetFontFromSystemFonts()
			If systemFontRadioButton.Checked Then
				Dim fontName As String = DirectCast(fontsComboBox.SelectedItem, String)
				Try
					Dim font As PdfFont
					If fontTypeComboBox.SelectedIndex = 0 Then
						font = _tempDocument.FontManager.CreateSimpleFontFromTrueTypeFont(PdfFontManager.GetSystemTrueTypeFontFileName(fontName))
					Else
						font = _tempDocument.FontManager.CreateCIDFontFromTrueTypeFont(PdfFontManager.GetSystemTrueTypeFontFileName(fontName))
					End If
					pdfFontViewerControl.PdfFont = font
				Catch ex As Exception
					pdfFontViewerControl.PdfFont = Nothing
					DemosTools.ShowErrorMessage(ex)
				End Try
			End If
		End Sub

		Private Sub SetFontInFontViewer()
			If fromTTFRadioButton.Checked Then
				Dim font As PdfFont
				If fontTypeComboBox.SelectedIndex = 0 Then
					font = _tempDocument.FontManager.CreateCIDFontFromTrueTypeFont(_ttfStream)
				Else
					font = _tempDocument.FontManager.CreateSimpleFontFromTrueTypeFont(_ttfStream)
				End If
				pdfFontViewerControl.PdfFont = font
			Else
				If fontsComboBox.SelectedIndex >= 0 Then
					If systemFontRadioButton.Checked Then
						SetFontFromSystemFonts()
					ElseIf fromPDFDocumentRadioButton.Checked Then
						pdfFontViewerControl.PdfFont = _selectedDocument.Fonts(fontsComboBox.SelectedIndex)
					ElseIf standardFontRadioButton.Checked Then
						pdfFontViewerControl.PdfFont = _tempDocument.FontManager.GetStandardFont(DirectCast(fontsComboBox.SelectedIndex, StandardFontType))
					End If
				End If
			End If
		End Sub

		Private Sub FreeResources()
			If _ttfStream IsNot Nothing Then
				_ttfStream.Close()
				_ttfStream.Dispose()
			End If
			If _selectedDocument IsNot Nothing Then
				_selectedDocument.Dispose()
			End If
			_tempDocument.Dispose()
		End Sub

		#End Region

	End Class
End Namespace
